package com.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.modle.User;

public class DBDao {
	
	
	static final String TAG = "DBManager";
	static final String DB_NAME = "iyr.db";
	static final String C_ID = "id";// 数据记录ID
	static final String C_UID = "uid";// 艺人用户ID
	static final String C_FID = "fid";// 最近联系人ID
	static final String C_MOBILE = "mobile";// 手机
	static final String C_NAME = "name";// 名称
	static final String C_NICKNAME = "nickname";// 别名
	static final String C_THUMB = "thumb";// 头像
	static final String C_TYPE = "u_type";// 是艺人还是机构
	static final String C_YRH = "yrh";// 艺人号
	static final String C_SIGN = "sign";// 签名
	static final String C_SEX = "sex";// 性别
	static final String C_PLACE = "place";// 地点
	static final String C_ISMEMBER = "is_member";// 地点
	static final String C_RECETIME = "rece_time";// 最近联系的时间
	/*********** 消息START ***************************************************************/
	static final String C_M_TOTALNUM = "msg_total";// 消息数量
	static final String C_M_TYPE = "msg_type";// 消息类型
	static final String C_M_CONTENT = "msg_content";// 消息内容
	// static final String C_M_ISREAD = "msg_isread";// 最近联系的时间
	/*********** 消息END ***************************************************************/
	static final String TAB_NAME_RECENT_USER = "recent_user";// 最近联系人
	DBHelper dbHelper;
	SQLiteDatabase db;
	Context context;

	static String createTab_RecentUser = "create table " + TAB_NAME_RECENT_USER
			+ "(" + C_ID + " Integer  PRIMARY KEY autoincrement," + C_UID
			+ " varchar(20)," + C_FID + " varchar(20)," + C_MOBILE
			+ " varchar(20)," + C_NAME + " varchar(20)," + C_NICKNAME
			+ " varchar(30)," + C_THUMB + " varchar(20)," + C_TYPE
			+ " varchar(20)," + C_SEX + " varchar(5)," + C_YRH
			+ " varchar(20)," + C_SIGN + " varchar(200)," + C_PLACE
			+ " varchar(20)," + C_RECETIME + " varchar(20)," + C_ISMEMBER
			+ " varchar(5)," + C_M_TOTALNUM + " varchar(5)," + C_M_TYPE
			+ " varchar(15)," + C_M_CONTENT + " varchar(100)"
			// + C_M_ISREAD + " varchar(5)"

			+ ");";

	public DBDao(Context context) {
		super();
		this.context = context;
		dbHelper = new DBHelper(context);
	}

	public static class DBHelper extends SQLiteOpenHelper {
		private static final int VERSION = 1;

		/**
		 * @param context
		 * @param name
		 * @param factory
		 * @param version
		 */
		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		public DBHelper(Context context, String name, int version) {
			this(context, name, null, version);
		}

		public DBHelper(Context context, String name) {
			this(context, name, VERSION);
		}

		public DBHelper(Context context) {
			this(context, DB_NAME, VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			System.out.println("create a database");
			// execSQL用于执行SQL语句
			Log.d(TAG, "onCreate--->" + createTab_RecentUser);
			db.execSQL(createTab_RecentUser);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			System.out.println("upgrade a database");

		}

	}

	public void open() {
		db = dbHelper.getWritableDatabase();
	}

	public long insertRecentUser(String uid, String recetime, User u) {
		/*
		 * // 创建ContentValues对象 ContentValues values = new ContentValues(); //
		 * 向该对象中插入键值对，其中键是列名，值是希望插入到这一列的值，值必须和数据库当中的数据类型一致 values.put("id",
		 * user.id); values.put("name", user.name); db.insert("user", null,
		 * values);
		 */
		ContentValues cv = new ContentValues();
		cv.put(C_UID, uid);
		cv.put(C_FID, u.id);
		cv.put(C_MOBILE, u.mobile);
		cv.put(C_NAME, u.name);
		cv.put(C_NICKNAME, u.nickName);
		cv.put(C_THUMB, u.thumb);
		cv.put(C_TYPE, u.type);
		cv.put(C_SEX, u.male);
		cv.put(C_YRH, u.iyrNum);
		cv.put(C_SIGN, u.signature);
		cv.put(C_PLACE, u.place);
		cv.put(C_ISMEMBER, u.isMember);

		cv.put(C_RECETIME, recetime);

		cv.put(C_M_TOTALNUM, u.msgTotalNum);
		cv.put(C_M_TYPE, u.msgType);
		cv.put(C_M_CONTENT, u.msgContent);
		// cv.put(C_M_ISREAD, u.isRead);

		return db.insert(TAB_NAME_RECENT_USER, null, cv);

	}

	public int updateRecentUser(String uid, String recetime, User u) {
		ContentValues cv = new ContentValues();
		cv.put(C_UID, uid);
		cv.put(C_FID, u.id);
		cv.put(C_MOBILE, u.mobile);
		cv.put(C_NAME, u.name);
		cv.put(C_NICKNAME, u.nickName);
		cv.put(C_THUMB, u.thumb);
		cv.put(C_TYPE, u.type);
		cv.put(C_SEX, u.male);
		cv.put(C_YRH, u.iyrNum);
		cv.put(C_SIGN, u.signature);
		cv.put(C_PLACE, u.place);
		cv.put(C_ISMEMBER, u.isMember);

		cv.put(C_RECETIME, recetime);

		cv.put(C_M_TOTALNUM, u.msgTotalNum);
		cv.put(C_M_TYPE, u.msgType);
		cv.put(C_M_CONTENT, u.msgContent);
		// cv.put(C_M_ISREAD, u.isRead);
		return db.update(TAB_NAME_RECENT_USER, cv, C_UID + "=? AND " + C_FID
				+ "= ?", new String[] { uid, u.id });

	}

	public int delRecentUser(String uid, String fid) {
		return db.delete(TAB_NAME_RECENT_USER, C_UID + "=? AND " + C_FID
				+ "= ?", new String[] { uid, fid });
	}

	/**
	 * 
	 * @Description:TODO主键删除
	 * @param@param id
	 * @param@return
	 * @return int
	 * @author zhuw
	 * @date 2013-8-22 下午5:04:54
	 */
	public int delRecentUser(int id) {
		return db.delete(TAB_NAME_RECENT_USER, C_ID + "=?",
				new String[] { String.valueOf(id) });
	}

	/**
	 * 
	 * @Description:TODO判断是否存在记录
	 * @param@param uid
	 * @param@param fid
	 * @param@return
	 * @return int
	 * @author zhuw
	 * @date 2013-8-22 下午5:06:22
	 */
	public int hasRecord(String uid, String fid) {

		Cursor cursor = db.query(TAB_NAME_RECENT_USER, new String[] { C_FID },
				C_UID + "=? and " + C_FID + "=?", new String[] { uid, fid },
				null, null, null);
		cursor.moveToFirst();
		return cursor.getCount();
	}

	public ArrayList<User> selectAllRecent(String uid) {
		ArrayList<User> list = new ArrayList<User>();
		// 调用SQLiteDatabase对象的query方法进行查询，返回一个Cursor对象：由数据库查询返回的结果集对象
		// 第一个参数String：表名
		// 第二个参数String[]:要查询的列名
		// 第三个参数String：查询条件
		// 第四个参数String[]：查询条件的参数
		// 第五个参数String:对查询的结果进行分组
		// 第六个参数String：对分组的结果进行限制
		// 第七个参数String：对查询的结果进行排序
		Cursor cursor = db.query(TAB_NAME_RECENT_USER, new String[] { C_FID,
				C_MOBILE, C_NAME, C_NICKNAME, C_THUMB, C_TYPE, C_YRH, C_SIGN,
				C_PLACE, C_SEX, C_RECETIME, C_ISMEMBER, C_M_TOTALNUM, C_M_TYPE,
				C_M_CONTENT }, C_UID + "=?", new String[] { uid }, null, null,
				null);

		// 将光标移动到下一行，从而判断该结果集是否还有下一条数据，如果有则返回true，没有则返回false
		// cursor.moveToFirst();
		System.out.println(cursor.getCount());

		while (cursor.moveToNext()) {
			User u = new User();
			u.id = cursor.getString(cursor.getColumnIndex(C_FID));
			u.mobile = cursor.getString(cursor.getColumnIndex(C_MOBILE));
			u.name = cursor.getString(cursor.getColumnIndex(C_NAME));
			u.nickName = cursor.getString(cursor.getColumnIndex(C_NICKNAME));
			u.thumb = cursor.getString(cursor.getColumnIndex(C_THUMB));
			u.type = cursor.getString(cursor.getColumnIndex(C_TYPE));
			u.place = cursor.getString(cursor.getColumnIndex(C_PLACE));
			u.signature = cursor.getString(cursor.getColumnIndex(C_SIGN));
			u.male = cursor.getString(cursor.getColumnIndex(C_SEX));
			u.recetime = cursor.getString(cursor.getColumnIndex(C_RECETIME));
			if ("1".equalsIgnoreCase(cursor.getString(cursor
					.getColumnIndex(C_ISMEMBER)))) {
				u.isMember = true;
			} else {
				u.isMember = false;

			}

			u.msgTotalNum = cursor.getString(cursor
					.getColumnIndex(C_M_TOTALNUM));
			u.msgType = cursor.getString(cursor.getColumnIndex(C_M_TYPE));
			u.msgContent = cursor.getString(cursor.getColumnIndex(C_M_CONTENT));

			list.add(u);
		}
		return list;
	}

	/**
	 * 
	 * @Description:TODO根据类型type 查询
	 * @param@param uid
	 * @param@param type
	 * @param@return
	 * @return ArrayList<User>
	 * @author zhuw
	 * @date 2013-8-24 下午7:17:08
	 */
	public ArrayList<User> selectAllRecentByType(String uid, String type) {
		ArrayList<User> list = new ArrayList<User>();
		// 调用SQLiteDatabase对象的query方法进行查询，返回一个Cursor对象：由数据库查询返回的结果集对象
		// 第一个参数String：表名
		// 第二个参数String[]:要查询的列名
		// 第三个参数String：查询条件
		// 第四个参数String[]：查询条件的参数
		// 第五个参数String:对查询的结果进行分组
		// 第六个参数String：对分组的结果进行限制
		// 第七个参数String：对查询的结果进行排序
		Cursor cursor = db.query(TAB_NAME_RECENT_USER, new String[] { C_FID,
				C_MOBILE, C_NAME, C_NICKNAME, C_THUMB, C_TYPE, C_YRH, C_SIGN,
				C_PLACE, C_SEX, C_RECETIME, C_ISMEMBER, C_M_TOTALNUM, C_M_TYPE,
				C_M_CONTENT }, C_UID + "=? and " + C_TYPE + "=?", new String[] {
				uid, type }, null, null, null);
		// 将光标移动到下一行，从而判断该结果集是否还有下一条数据，如果有则返回true，没有则返回false
		// cursor.moveToFirst();
		System.out.println(cursor.getCount());

		while (cursor.moveToNext()) {
			User u = new User();
			u.id = cursor.getString(cursor.getColumnIndex(C_FID));
			u.mobile = cursor.getString(cursor.getColumnIndex(C_MOBILE));
			u.name = cursor.getString(cursor.getColumnIndex(C_NAME));
			u.nickName = cursor.getString(cursor.getColumnIndex(C_NICKNAME));
			u.thumb = cursor.getString(cursor.getColumnIndex(C_THUMB));
			u.type = cursor.getString(cursor.getColumnIndex(C_TYPE));
			u.place = cursor.getString(cursor.getColumnIndex(C_PLACE));
			u.signature = cursor.getString(cursor.getColumnIndex(C_SIGN));
			u.male = cursor.getString(cursor.getColumnIndex(C_SEX));
			u.recetime = cursor.getString(cursor.getColumnIndex(C_RECETIME));
			if ("1".equalsIgnoreCase(cursor.getString(cursor
					.getColumnIndex(C_ISMEMBER)))) {
				u.isMember = true;
			} else {
				u.isMember = false;

			}

			// u.msgTotalNum = "10";
			u.msgTotalNum = cursor.getString(cursor
					.getColumnIndex(C_M_TOTALNUM));
			u.msgType = cursor.getString(cursor.getColumnIndex(C_M_TYPE));
			// u.msgContent = "好消息好消息好消息好消息好消息好消息";
			u.msgContent = cursor.getString(cursor.getColumnIndex(C_M_CONTENT));

			list.add(u);
		}
		return list;
	}

	public void close() {
		db.close();
	}

}
